import { create } from 'zustand'
import { persist } from 'zustand/middleware'

// import type {  } from '@/api/modules/login'

interface ICommonState {
  token: string
  setToken: (val: string) => void
  clearToken: () => void

  jti: string
  setJti: (val: string) => void
  clearJti: () => void

  userInfo: UserInfo | anyObj
  setUserInfo: (val: any) => void
  clearUserInfo: () => void

  userId: string
  setUserId: (val: string) => void

  /** 是否已经登录 */
  isLogin: () => boolean

}

export interface UserInfo {
  id: string
  userId: string
  companyId: string
  companyName: string
  deptId: string
  deptName: string
  userName: string
  roleName: string
  surname: any
  name: any
  employeeId: any
  userType: string
  email: string
  phonenumber: string
  sex: string
  country: string
  birthday: string
  idcardNo: string
  idcardExpired: string
  status: string
  loginIp: string
  loginDate: any
  insuranceBalanceAmount: number
  businessBalanceAmount: number
  memberLevel: string
  userCardList: any
  isCreditLimit: boolean
  roleId: string
  /** 是否已授权保险 '1'是 */
  insuranceAuthStatus: string
  /** 是否存在密码 '1'存在  '0'不存在 */
  existPassword: string
}

export const useCommonStore = create<ICommonState>()(
  persist(
    (set, get) => ({
      token: '',
      setToken: (val: string) => {
        set((state) => {
          return { token: val }
        })
      },
      clearToken: () => {
        set(state => ({ token: '' }))
      },

      jti: '',
      setJti: (val: string) => {
        set((state) => {
          return { jti: val }
        })
      },
      clearJti: () => {
        set(state => ({ jti: '' }))
      },

      userInfo: {},
      setUserInfo(val) {
        set(() => ({ userInfo: val }))
      },
      clearUserInfo() {
        set(() => ({ userInfo: {} }))
      },

      userId: '',
      setUserId(val) {
        set(() => ({ userId: val }))
      },

      isLogin(): boolean {
        return get().token ? true : false
      },

    }),
    { name: 'common_store' },
  ),
)
